package com.skynet.supervision.runner;

import com.skynet.supervision.beans.response.SysDictDataVo;
import com.skynet.supervision.dao.SysDictDataMapper;
import com.skynet.supervision.utils.DictUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class LoadDictRunner implements ApplicationRunner {

    private final SysDictDataMapper sysDictDataMapper;

    public LoadDictRunner(SysDictDataMapper sysDictDataMapper) {
        this.sysDictDataMapper = sysDictDataMapper;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("load dict data to cache ... ");
        val dictDataMap = sysDictDataMapper.find(null)
                .stream()
                .collect(Collectors.groupingBy(SysDictDataVo::getKey));
        for (Map.Entry<String, List<SysDictDataVo>> entry : dictDataMap.entrySet()) {
            DictUtils.setDictCache(entry.getKey(), entry.getValue());
        }
    }
}
